package cc.wfu.type.service;

import cc.wfu.type.trigger.api.dto.JwtTokenRequest;
import cc.wfu.type.trigger.api.dto.VerifyJwtTokenRequest;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.JwtException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Objects;


/**
 * @Author: jia-qiang ljq1024.cc
 * @desc: 权限
 * @Date: 2024-05-21-23:11
 */
@Slf4j
@Service
public class JwtAuthService extends JwtAuthServiceSupport {

    /**
     * 生成 - jwtToken
     * @param jwtTokenRequest subject 参数
     */
    public String genJwtToken(JwtTokenRequest jwtTokenRequest) {
        log.info("生成 - jwtToken - 开始");
        // 1. 参数校验
        if (!StringUtils.hasLength(jwtTokenRequest.getSubject())) {
            log.warn("生成 - jwtToken - 参数异常【subject is null】, {}", jwtTokenRequest);
            return null;
        }
        JSONObject jsonObject = JSON.parseObject(jwtTokenRequest.getSubject(), JSONObject.class);
        Object username =  jsonObject.get("username");
        Object password = jsonObject.get("password");
        if (Objects.isNull(username) || Objects.isNull(password)) {
            throw new JwtException("please provide properties【username(用户名) and password(明文密码)】. your subject not contain -> " + jsonObject);
        }
        // 2. 验证SpringSecurity
        authenticate(username.toString(), password.toString());
        // 3. 生成jwt并返回
        String jwtToken = jwtUtil.create(jwtTokenRequest.getSubject());
        log.info("生成 - jwtToken - 结束: {}", jwtToken);
        return jwtToken;
    }

    /**
     * 解析 - jwtToken
     * @param verifyJwtTokenRequest token 参数
     */
    public String verifyJwtToken(VerifyJwtTokenRequest verifyJwtTokenRequest) {
        try {
            log.info("解析 - jwtToken - 开始");

            // 1. 参数校验
            if (!StringUtils.hasLength(verifyJwtTokenRequest.getJwtToken())) {
                log.warn("解析 - jwtToken - 参数异常【jwtToken is null】, {}", verifyJwtTokenRequest);
            }
            // 2. 解析jwt并返回
            Claims parse = jwtUtil.parse(verifyJwtTokenRequest.getJwtToken());
            String userEntityJsonStr = parse.getSubject();
            log.info("解析 - jwtToken - 结束: {}", userEntityJsonStr);
            return userEntityJsonStr;
        }catch (Exception e) {
            log.error("解析 - jwtToken - 失败 - error: ", e);
            return null;
        }
    }

    /**
     * 解析属性 - jwtToken
     * @param verifyJwtTokenRequest token 参数
     */
    public String getUserId(VerifyJwtTokenRequest verifyJwtTokenRequest, String property) {
        try {
            log.info("解析用户ID - jwtToken - 开始");

            // 1. 参数校验
            if (!StringUtils.hasLength(verifyJwtTokenRequest.getJwtToken())) {
                log.warn("解析用户ID - jwtToken - 参数异常【jwtToken is null】, {}", verifyJwtTokenRequest);
            }
            // 2. 解析jwt并返回
            Claims parse = jwtUtil.parse(verifyJwtTokenRequest.getJwtToken());
            String userEntityJsonStr = parse.getSubject();
            JSONObject jsonObject = JSON.parseObject(userEntityJsonStr);
            Object object = jsonObject.get(property);
            log.info("解析用户ID - jwtToken - 结束: {}", userEntityJsonStr);
            return object.toString();
        }catch (Exception e) {
            log.error("解析用户ID - jwtToken - 失败 - error: ", e);
            return null;
        }
    }

}
